package com.bzzoneit.newsys.web.admin;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bzzoneit.newsys.entity.admin.User;
import com.bzzoneit.newsys.service.admin.UserService;
import com.bzzoneit.newsys.utils.dwz.AjaxDone;
import com.bzzoneit.newsys.utils.email.MailService;
import com.bzzoneit.newsys.utils.excel.XlsExport;
import com.bzzoneit.newsys.utils.page.Page;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User> {
	
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private MailService mailService;
	
	/**
	 * page 获得从页面传过来的分页信息
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("/list")
	public String list(@ModelAttribute("page") Page<User> page,
			@ModelAttribute("user") User user,
			@RequestParam("navtabid") String navtabid,
			Model model) {
		logger.info("start user list : pageisze:"+page.getNumPerPage());
		page.setT(user);
		page=userService.getUserList(page);
		model.addAttribute("page", page);
		model.addAttribute("user", user);
		model.addAttribute("navtabid", navtabid);
		return "user/list";
	}

	@ResponseBody
	@RequestMapping("/insert")
	public AjaxDone insert(@ModelAttribute("user")User user,
			@RequestParam("navtabid") String navtabid) {
		userService.insertUser(user);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("新增用户成功");
		a.setForwardUrl(""); 
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	@ResponseBody
	@RequestMapping("/update")
	public AjaxDone update(@ModelAttribute("user")User user,
			@RequestParam("navtabid") String navtabid
			) 
	{
		userService.updateUser(user);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setCallbackType("closeCurrent");
		a.setMessage("更新用户id的"+user.getId()+"成功");
		a.setForwardUrl("");
		a.setNavTabId(navtabid); //重新加载这个nav
		return a;
	}
	
	@ResponseBody
	@RequestMapping("/delete/{id}")
	public AjaxDone delete(@PathVariable("id") Long id) {
		userService.deleteUser(id);
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setMessage("删除成功");
		return a;
	}
	
	
	@ResponseBody
	@RequestMapping("/changepassword/{id}")
	public AjaxDone changepassword(@PathVariable("id") Long id) {
		User u=userService.selectOne(id);
		//生成新的密码
		UUID uuid  =  UUID.randomUUID(); 
		String s = UUID.randomUUID().toString();
		String newPassword=s.substring(0, 8);//取前8位
		u.setPassword(newPassword);
		userService.updateUser(u);
		//发送邮件
		mailService.sendNotificationMail(u.getName(), newPassword, u.getEmail());
		AjaxDone a=new AjaxDone();
		a.setStatusCode(200);
		a.setMessage(u.getName()+"用户更新密码邮件发送成功");
		return a;
	}
	
	@RequestMapping(value="/edit/{id}")
	public String prepare(Model model,@PathVariable("id") String id,
			@RequestParam("navtabid") String navtabid
		) {
		model.addAttribute("navtabid", navtabid);
		User u=new User();
		u.setId(Long.parseLong(id));
		u=userService.login(u);
		model.addAttribute("user", u);
		return "user/edit";
	}

	@RequestMapping(value="/add")
	public String add(Model model,@RequestParam("navtabid") String navtabid) {
		model.addAttribute("navtabid", navtabid);
		return "user/add";
	}
	
	@RequestMapping(value="/advancedSearch")
	public String advancedSearch(Model model,@RequestParam("navtabid") String navtabid) {
		model.addAttribute("navtabid", navtabid);
		return "user/advancedSearch";
	}
	
	@RequestMapping(value="/save")
	public String save(@ModelAttribute("user")User user) {		
		if(user.getId()==null){
			userService.insertUser(user);}
		else
		{
			userService.updateUser(user);
		}
		return "redirect:list";
	}
	
	
	@RequestMapping(value="/exportPDF")
	public ModelAndView exportPDF(HttpServletRequest request,  
	        HttpServletResponse response) {	
		response.setContentType("Application/excel");
		response.addHeader("Content-Disposition",
				"attachment;filename=userList.xls");
		XlsExport e = new XlsExport();
		int rowIndex = 0;
		e.createRow(rowIndex);
		e.setCell(0, "姓名");
		e.setCell(1, "年龄");
		e.createRow(++rowIndex);
		e.setCell(0, "张三");
		e.setCell(1, "22");
		e.createRow(++rowIndex);
		e.setCell(0, "李四");
		e.setCell(1, "25");
		e.exportXls(response);
		return null;
	}
	
}
